package modulesAPI.v2.basics;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;

import com.caterina.components.CComboBox;
import com.caterina.components.CLabel;
import com.caterina.components.CPanel;
import com.caterina.resources.CModules;
import com.caterina.resources.CWords;

import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;

public class ModComboBox extends Module<Enum<?>> implements ActionListener{
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un enum
	 * */
	public ModComboBox(Module<?> ownerModule, Enum<?> content, String tag,String description, ViewType viewType,Enum<?>...values) {
		super(ownerModule, content, tag, description, viewType);
		this.translations=new HashMap<String, Enum<?>>();
		this.translatedArray=new ArrayList<String>();
		for(Enum<?> e:values){
			translations.put(CWords.getWord(e.name()), e);
			translatedArray.add(CWords.getWord(e.name()));
		}
		this.comboBox=new CComboBox(translatedArray);
		this.comboBox.setSelectedItem(CWords.getWord(this.getContent().name()));
		this.comboBox.addActionListener(this);
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		return CModules.formatPanelForReadOnlyPairs(this.getTag(), CWords.getWord(this.getContent().name()));
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		return CModules.formatForPairs(this.getTag(), comboBox);
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
		CPanel retPanel=new CPanel();
		retPanel.add(new CLabel(this.getContent().name()));
		return retPanel;
	}
	private Enum<?> getSelectedItem(){
		return this.translations.get(this.comboBox.getSelectedItem());
	}
	public int getHierachyNumber(){return -1;}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		this.setContent(this.getSelectedItem());
		this.fireModuleChanged(this);
	}
	@Override
	protected void beforeFireChange(Module<?> mod) {
	}

	@Override
	protected void afterFireChange(Module<?> mod) {
		
	}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return this.getReadOnlyPanel(isSelected);
	}
	private ArrayList<String> translatedArray;
	private HashMap<String,Enum<?>> translations;//Parola chiave : parola tradotta
	private CComboBox comboBox;
}
